// Copyright 2012 Square, Inc.
package com.squareup.timessquare;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;

import com.mycjj.android.R;
import com.mycjj.android.util.Preferences;
import com.squareup.timessquare.bean.MonthCellDescriptor;
import com.squareup.timessquare.bean.MonthDescriptor;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MonthView extends LinearLayout {
  TextView title;
  CalendarGridView grid;
  private Listener listener;

  private static boolean isSign = true;
  public static MonthView create(ViewGroup parent, LayoutInflater inflater,
      DateFormat weekdayNameFormat, Listener listener, Calendar today,boolean isSigned) {
	isSign = isSigned;
    final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent, false);

    final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

    int firstDayOfWeek = today.getFirstDayOfWeek();
    final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
    headerRow.setIsSign(false);
    String weekName = "";
    for (int offset = 0; offset < 7; offset++) {
      today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
      FrameLayout frameLayout = (FrameLayout)headerRow.getChildAt(offset);
      final TextView textView = (TextView) frameLayout.getChildAt(0);
      weekName = weekdayNameFormat.format(today.getTime());
      textView.setText(weekName.substring(2, weekName.length()));
      textView.setGravity(Gravity.CENTER);
    }
    today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
    view.listener = listener;
    return view;
  }

  public MonthView(Context context, AttributeSet attrs) {
    super(context, attrs);
    
  }

  @Override protected void onFinishInflate() {
    super.onFinishInflate();
    title = (TextView) findViewById(R.id.title);
    grid = (CalendarGridView) findViewById(R.id.calendar_grid);
  }

  public void init(MonthDescriptor month, List<List<MonthCellDescriptor>> cells,boolean isSign) {
    Logr.d("Initializing MonthView (%d) for %s", System.identityHashCode(this), month);
    long start = System.currentTimeMillis();
    String label = month.getLabel();
    title.setText(label);
    Log.e("mark", "label:"+label);
    final int numRows = cells.size();
    grid.setNumRows(numRows);
    for (int i = 0; i < 6; i++) {
      CalendarRowView weekRow = (CalendarRowView) grid.getChildAt(i + 1);
      weekRow.setIsSign(isSign);
      weekRow.setListener(listener);
      if (i < numRows) {
        weekRow.setVisibility(VISIBLE);
        List<MonthCellDescriptor> week = cells.get(i);
        for (int c = 0; c < week.size(); c++) {
          MonthCellDescriptor cell = week.get(c);
          Log.d("mark",".......................");
          Log.d("mark","cell:"+cell.toString());
          Log.d("mark",".......................");
          FrameLayout flview = (FrameLayout)weekRow.getChildAt(c);
          CalendarCellView cellView = (CalendarCellView) flview.getChildAt(0);
          TextView textview = (TextView)flview.getChildAt(1);
          cellView.setText(Integer.toString(cell.getValue()));
          cellView.setEnabled(cell.isCurrentMonth());//是否当前月份
          flview.setVisibility(cell.isCurrentMonth() ? View.VISIBLE:View.GONE);
          
          boolean isSelectable = cell.isSelectable();
          cellView.setSelectable(isSelectable);
          if(isSign){
        	  if(cell.isSigned()){//是否已签到
        		 String points =  Preferences.getSharedPreferences(getContext(), "sign_score", "5");
        		 textview.setText("+"+points+"元宝");
        		 textview.setTextSize(12);
        		 textview.setVisibility(View.VISIBLE);
        	  }else{
        		  textview.setVisibility(View.GONE);
        	  }
        	  cellView.setBackgroundResource(R.color.transparent);
        	  cellView.setTextColor(Color.parseColor("#797979"));
          }else{
        	  textview.setVisibility(View.GONE);
        	  if(isSelectable){
            	  int daySate = cell.getDayState();
            	  switch(daySate){
            	  case 0:
            		  cellView.setBackgroundResource(R.drawable.calendar_cell_green_bg);
            		  break;
            	  case 1:
            		  cellView.setBackgroundResource(R.drawable.calendar_cell_yellow_bg);
            		  break;
            	  case 2:
            		  cellView.setSelectable(false);
            		  cellView.setBackgroundResource(R.drawable.calendar_cell_red_bg);
            		  break;
            	  }
              }else{
            	  cellView.setBackgroundResource(R.drawable.calendar_cell_gray_bg);
              }
          }
          
          
          cellView.setSelected(cell.isSelected());
          cellView.setCurrentMonth(cell.isCurrentMonth());
          cellView.setToday(cell.isToday());
          cellView.setRangeState(cell.getRangeState());
          cellView.setHighlighted(cell.isHighlighted());
          cellView.setTag(cell);
          flview.setTag(cell);
        }
      } else {
        weekRow.setVisibility(GONE);
      }
    }
    Logr.d("MonthView.init took %d ms", System.currentTimeMillis() - start);
  }

  public interface Listener {
    void handleClick(MonthCellDescriptor cell);
  }
}
